package lxh.circleloadingview.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;



/**
 * Created by liuxh on 2017/7/31.
 */

public class CircleLoadingView extends RelativeLayout {
    private static final String TAG = "CircleLoadingView";
    private static final long DURATION_TIME = 500;
    private Context mContext;
    private CircleView mLeftView, mMiddleView, mRightView;

    private int mTranslationDistance;
    private boolean mIsStopAnimation=false;
    public CircleLoadingView(Context context) {
        this(context, null);
    }

    public CircleLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.d(TAG, "CircleLoadingView: ");
        mContext=context;
        mTranslationDistance=dipToPx(30);
        setBackgroundColor(Color.WHITE);
        initLayout();
    }

    private void initLayout() {
        //创建三个小圆，添加进来
        mLeftView = getCircleView();
        mLeftView.exchangeColor(Color.GRAY);

        mMiddleView = getCircleView();
        mMiddleView.exchangeColor(Color.RED);

        mRightView = getCircleView();
        mRightView.exchangeColor(Color.BLUE);

        addView(mLeftView);
        addView(mRightView);
        addView(mMiddleView);

        //启动展开时动画属性
        post(new Runnable() {
            @Override
            public void run() {
                expandAnimation();
            }
        });
    }

    private void expandAnimation() {
        if (mIsStopAnimation)return;
        Log.d(TAG, "expandAnimation: ");
        ObjectAnimator leftTransmation=ObjectAnimator.ofFloat(mLeftView,"translationX",0,-mTranslationDistance);
        ObjectAnimator rightTransmation=ObjectAnimator.ofFloat(mRightView,"translationX",0,mTranslationDistance);

        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.setDuration(DURATION_TIME);
        animatorSet.playTogether(leftTransmation,rightTransmation);
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //收缩动画
                shrinkAnimation();
            }
        });
        animatorSet.start();
    }

    private void shrinkAnimation() {
        if (mIsStopAnimation)return;
        Log.d(TAG, "shrinkAnimation: ");
        ObjectAnimator leftTransmation=ObjectAnimator.ofFloat(mLeftView,"translationX",0,mTranslationDistance);
        ObjectAnimator rightTransmation=ObjectAnimator.ofFloat(mRightView,"translationX",0,-mTranslationDistance);

        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.setDuration(DURATION_TIME);
        animatorSet.playTogether(leftTransmation,rightTransmation);
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //变换圆形颜色,左给中，中给右，右给左
                int leftColor=mLeftView.getCurrentColor();
                int middleColor=mMiddleView.getCurrentColor();
                int rightColor=mRightView.getCurrentColor();
                mMiddleView.exchangeColor(leftColor);
                mRightView.exchangeColor(middleColor);
                mLeftView.exchangeColor(rightColor);
                //展开动画
                expandAnimation();
            }
        });
        animatorSet.start();
    }

    public CircleView getCircleView() {
        CircleView circleView = new CircleView(mContext);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(dipToPx(10), dipToPx(10));
        params.addRule(CENTER_IN_PARENT);
        circleView.setLayoutParams(params);
        return circleView;
    }

    private int dipToPx(int i) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, i, getResources().getDisplayMetrics());
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(INVISIBLE);
        mLeftView.clearAnimation();
        mRightView.clearAnimation();
        ViewGroup parent = (ViewGroup) getParent();
        if (parent!=null){
            parent.removeView(this);
            removeAllViews();
        }
        mIsStopAnimation=true;
    }
}
